#coding:utf-8
# 把cookie写入

import requests
import json
from operation_json import OperetionJson,Path


class OperationCookie:

	def __init__(self,res):
		self.res =res

	# def get_response_url(self):
	# 	# 获取登录返回的token的url
	# 	url = self.response['data']['url'][0]
	# 	return url

	def get_cookie(self):
		# 获取cookie的jar文件
		# url = self.get_response_url()+"&callback=jQuery21008240514814031887_1508666806688&_=1508666806689"
		# cookie = requests.get(url).cookies
		cookie_jar = self.res.cookies
		return cookie_jar


	def write_cookie(self):
		# 转换cookie为json
		cookie_json = requests.utils.dict_from_cookiejar(self.get_cookie())
		# cookie文件需要传参
		op_json = OperetionJson(Path.path_cookie)
		op_json.write_data(cookie_json)
		
if __name__ == '__main__':
	
	url = "http://m.imooc.com/passport/user/login"
	data = {
		"username":"18513199586",
		"password":"111111",
		"verify":"",
		"referer":"https://m.imooc.com"
	}
	res = json.dumps(requests.post(url,data).json())
	op_Cookie = OperationCookie(res)
	op_Cookie.write_cookie()
